package com.xiaohu.mayi_huiyuan.controller;

import com.xiaohu.mayi_huiyuan.entity.Customer;
import com.xiaohu.mayi_huiyuan.service.RegisterAndConsumption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class CustomerRegisterController {

    private final static Logger logger = LoggerFactory.getLogger(Exception.class);


    @Autowired
    RegisterAndConsumption registerAndConsumption;


    @RequestMapping("/inregister")
    public String inRegister() {
        return "zhucexiao";
    }


    @RequestMapping("registerCustomer")
    @ResponseBody
    public String registerCustomer(Customer customer, HttpSession session) {
        registerAndConsumption.insertCustomer(customer, session);
        return "ok";
    }

    /**
     * 验证手机 是否被注册
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/vaphone")
    public String vaPhone(String phone) {
        logger.info("手机号码验证");
        if (registerAndConsumption.vaPhone(phone)) {
            return "no";
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/isexist")
    public String isExist(String phoneAndkahao) {
        return registerAndConsumption.isExist(phoneAndkahao);
    }


    /**
     * 充值
     *
     * @return
     */
    @RequestMapping("/insertXiaofei")
    @ResponseBody
    public String insertXiaofei(String phoneAndKahao, String miaoshu, String jine, HttpSession session) {
        jine = jine.trim();
        registerAndConsumption.insertChongZhi(phoneAndKahao, miaoshu, jine, session);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/insertxiaofei")
    public String xiaoFei(String phoneOrKahao, String jine, String miaoshu, HttpSession session) {
        logger.info("insertxiaofei:" + phoneOrKahao + "---" + jine + "---" + miaoshu);
        jine = jine.trim();
        registerAndConsumption.xiaoFei_FanXian(phoneOrKahao, Double.parseDouble(jine), miaoshu, session);
        return "ok";
    }

    @RequestMapping("/selectFanXianYue")
    @ResponseBody
    public String selectFanXianYue(String phoneOrKahao) {
        Double fanxianyue = registerAndConsumption.selectFanXianYue(phoneOrKahao);
        if (fanxianyue == null) {
            return "0";
        }
        return String.valueOf(fanxianyue);
    }

    /**
     * 通过phone or kahao 查询用户充值余额
     *
     * @return
     */
    @RequestMapping("/getCustomerChongZhiYue")
    @ResponseBody
    public String getCustomerChongZhiYue(String phoneOrKahao) {
        Double chongzhiyue = registerAndConsumption.selectChongZhiYue(phoneOrKahao);
        return chongzhiyue == null ? "0.0" : String.valueOf(chongzhiyue);
    }

    @ResponseBody
    @RequestMapping("/insertChongZhiXiaoFei")
    public String insertChongZhiXiaoFei(String phoneorkahao, String jine, String miaoshu, String password, HttpSession session) {
        logger.info("insertxiaofei:" + phoneorkahao + "---" + jine + "---" + miaoshu + "---" + password);
        return registerAndConsumption.insertChongZhiXiaoFei(phoneorkahao, jine, miaoshu, password, session);
    }


    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("/changePassword")
    @ResponseBody
    public String changePassword(String phoneOrKahao, String shenfenzheng, String xinmima, HttpSession session) {
        logger.info("insertxiaofei:" + phoneOrKahao + "---" + shenfenzheng + "---" + xinmima + "---");
        return registerAndConsumption.changePassword(phoneOrKahao, shenfenzheng, xinmima, session);
    }


}
